/*
 * Copyright (c) $2015 杨强辉<yangqianghui7788@gmail.com>. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package core.android.tools;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

/**
 * @version 1.0
 * @author: 杨强辉
 * @类 说 明: 文件目录管理
 * @创建时间：2014-11-7 下午9:18:20
 */
public class AKPathUtil {
    private static AKPathUtil instance = null;
    private File storageDir = null;//存储目录
    private File tempDir = null;
    private File voiceDir = null;
    private File imageDir = null;
    private File videoDir = null;
    private File fileDir = null;

    public static AKPathUtil getInstance() {
        if (instance == null) {
            instance = new AKPathUtil();
        }
        return instance;
    }

    public File getTempDir() {
        if (tempDir == null) {
            tempDir = new File(getStorageDir(), "/data/temp");
            if (!tempDir.exists() && !tempDir.mkdirs()) {
                AK.d( "建立音频目录失败！voiceDir=" + tempDir);
            }
        }
        return tempDir;
    }

    public File getVoiceDir() {
        if (voiceDir == null) {
            voiceDir = new File(getStorageDir(), "/data/voice");
            if (!voiceDir.exists() && !voiceDir.mkdirs()) {
                AK.d( "建立音频目录失败！voiceDir=" + voiceDir);
            }
        }
        return voiceDir;
    }

    public File getImageDir() {
        if (imageDir == null) {
            imageDir = new File(getStorageDir(), "/data/image");
        }
        if (!imageDir.exists() && !imageDir.mkdirs()) {
            AK.d( "建立图片目录失败！imageDir=" + imageDir);
        }
        return imageDir;
    }

    public File getVideoDir() {
        if (videoDir == null) {
            videoDir = new File(getStorageDir(), "/data/video");
        }
        if (!videoDir.exists() && !videoDir.mkdirs()) {
            AK.d( "建立视频目录失败！videoDir=" + videoDir);
        }
        return videoDir;
    }

    public File getFileDir() {
        if (fileDir == null) {
            fileDir = new File(getStorageDir(), "/data/file");
        }
        if (!fileDir.exists() && !fileDir.mkdirs()) {
            AK.d( "建立文件目录失败！fileDir=" + fileDir);
        }
        return fileDir;
    }

    private File getStorageDir() {
        if (storageDir == null) {
            String path = null;
            if (AKDeviceUtil.hasSDCard()) {
                try {
                    File cache = AKActivityUtil.INSTANCE.getApplication().getExternalCacheDir();
                    if (cache != null) {
                        path = cache.getAbsolutePath().replace("cache", "");
                    }
                } catch (Exception e) {
                    //小米2s获取存储卡的地址报空指针。
                    AK.d( "获取外置缓存目录发生异常！尝试建立外置缓存目录");
                    if (Environment.getExternalStorageDirectory().exists()) {
                        StringBuffer sb = new StringBuffer();
                        path = sb.append(Environment.getExternalStorageDirectory().getPath()).append("/Android/data/").append(AKActivityUtil.INSTANCE.getApplication().getPackageName()).toString();
                    } else {
                        path = AKActivityUtil.INSTANCE.getApplication().getCacheDir().getAbsolutePath().replace("cache", "");
                    }
                }
            } else {
                path = AKActivityUtil.INSTANCE.getApplication().getCacheDir().getAbsolutePath().replace("cache", "");
            }
            if (TextUtils.isEmpty(path)) {
                AK.d( "初始化存储目录失败！path=" + path);
                throw new RuntimeException("初始化存储目录失败！");
            } else {
                storageDir = new File(path);
                AK.d( "存储目录storageDir=" + storageDir);
            }
        }
        return storageDir;
    }

    /**
     * 清空临时目录下的文件
     */
    public void clearTempDir() {
        File[] files = AKPathUtil.getInstance().getTempDir().listFiles();
        for (File file : files) {
            file.delete();
        }
    }


    /**
     * 获取拍照的图片路径
     *
     * @return
     */
    @TargetApi(Build.VERSION_CODES.FROYO)
    public static File getDCIMFile() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    }

}
